﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using BLL;
using Model;
using BLL.RF.Repair;
using Model.RF.Repair;


public partial class ProductAssemble_ManageAssemble_QCFixedCheck_QCFixedCheck : System.Web.UI.Page
{
    private QCFixedCheck qc;
    private PCBAJointingCheck pjc;
    private ProductType_Flow pf;
    private Repair rep;
    private RepairRoute rroute;
    private List<QCFixedCheckInfo> list { get { return (List<QCFixedCheckInfo>)ViewState["checkinfolist"]; } set { ViewState["checkinfolist"] = value; } }

    public ProductAssemble_ManageAssemble_QCFixedCheck_QCFixedCheck()
    {
        qc = new QCFixedCheck();
        pjc = new PCBAJointingCheck();
        pf = new ProductType_Flow();
        rep = new Repair();
        rroute = new RepairRoute();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            list = new List<QCFixedCheckInfo>();
            BindJointingFailure();
            this.GridView1.DataSource = null;
            this.GridView1.DataBind();
        }
    }

    protected void BindJointingFailure()
    {
        JointingFailure jf = new JointingFailure();
        this.DropDownList1.DataSource = jf.JointingFailure_GetbyQCFix();
        this.DropDownList1.DataValueField = "FailureName";
        this.DropDownList1.DataTextField = "FailureName";
        this.DropDownList1.DataBind();
    }

    protected void TextBox2_TextChanged(object sender, EventArgs e)
    {
        string productSN = this.TextBox2.Text.Trim();
        productSN = CommHelper.GetProductSN(productSN);
        this.GridView1.DataSource = pjc.GetPCBAJointingChecksbyProductSN(productSN);
        this.GridView1.DataBind();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (!Add_QCFixedCheckInfo_tolist())
        {
            return;
        }
        foreach (QCFixedCheckInfo qci in list)
        {
            try
            {
                qc.Insert(qci);
            }
            catch
            {
                string str1 = "提交失败，请联系相关人员";
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
                return;
            }
        }
        Repair_AutoBuild(list);

        string str = "提交成功";
        ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
        this.GridView1.DataSource = null;
        this.GridView1.DataBind();
        list.Clear();
        this.Label1.Text = "";
        this.TextBox2.Text = "";
        this.TextBox2.Focus();
    }

    private void Repair_AutoBuild(List<QCFixedCheckInfo> list)
    {
        foreach (QCFixedCheckInfo qci in list)
        {
            if (qci.FailureName != "0.合格")
            {
                string repairPath = rroute.RepairRoute_GetbyStepItem("QCFixedCheck")[0].RepairPath;
                rep.Repair_AutoBuild(qci.ProductSN, repairPath, qci.UserID);
                return;
            }
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        string productSN = this.TextBox1.Text.Trim();
        productSN = CommHelper.GetProductSN(productSN);
        this.GridView2.DataSource = qc.GetQCFixedChecksbySN(productSN);
        this.GridView2.DataBind();
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        Add_QCFixedCheckInfo_tolist();
    }

    private bool Add_QCFixedCheckInfo_tolist()
    {
        string productTypeID = this.TextBox5.Text.Trim();
        string failureName = this.DropDownList1.SelectedValue;
        string responseUser = this.TextBox4.Text.Trim();
        if (failureName != "0.合格" && responseUser.Length != 6)
        {
            string str = "检验结果为不良时，需要录入相应责任人的六位工号，请确认！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
            return false;
        }
        if (failureName == "0.合格")
            responseUser = "";
        string productSN = this.TextBox2.Text.Trim();
        if (CommHelper.IsValidSN_forDataAcquisition(productSN))
        {
            //20170728，之前的S代码是从物料装配录入那里取的，现在修改成在页面增加录入
            //productSN = CommHelper.GetProductSN(productSN);
            //MO m = new MO();
            //House h = new House();
            //string moID = h.LoadEntity(productSN).MOID;
            //if (string.IsNullOrEmpty(moID))
            //{
            //    string str = "不能提交，此腔体没有赋予对应的MO号，请反查物料装配工序数据";
            //    ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
            //    return false;
            //}
            //string productTypeID = m.LoadEntity(moID).ProductTypeID;
            ProductType p = new ProductType();
            if (string.IsNullOrEmpty(p.LoadEntity(productTypeID).ProductTypeID))
            {
                string str = "产品类型不存在或未登记过！";
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
                return false;
            }
            if (!pf.ProductType_Flow_HasValidSevenStep(productSN, productTypeID, "QCFixedStep"))
            {
                string str = "不能提交，请反查此腔体的七大管控工序的数据!";
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
                return false;
            }
            else
            {
                QCFixedCheckInfo qci = new QCFixedCheckInfo(productSN, DateTime.Now, productTypeID, this.TextBox3.Text.Trim(), User.Identity.Name, failureName, responseUser);
                list.Add(qci);
                this.TextBox4.Text = "";
                Displayliststring();
                return true;
            }
        }
        else
        {
            string str = "序列号不符合腔体序列号编码规则，或不是关联过的成品序列号，请检查!";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
            return false;
        }
    }

    private void Displayliststring()
    {
        string liststring = "";
        foreach (QCFixedCheckInfo qci in list)
        {
            liststring = liststring + qci.FailureName + "：" + qci.ResponseUser + "；　";
        }
        this.Label1.Text = liststring;
    }
}
